package casa.depesos.app.com.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.common.base.base.BaseImageView;

import casa.depesos.app.com.R;


public class EqualRatioImageView extends BaseImageView {

    private float ratio;
    float max = 0.563f;
    boolean isRatio;

    public EqualRatioImageView(Context context, boolean isRatio) {
        this(context);
        this.isRatio = isRatio;
    }

    public EqualRatioImageView(Context context) {
        this(context, (AttributeSet) null);
    }

    public EqualRatioImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EqualRatioImageView);
        Integer height = a.getInteger(R.styleable.EqualRatioImageView_imageHeight, 1);
        Integer width = a.getInteger(R.styleable.EqualRatioImageView_imageWidth, 1);
        Integer r = a.getInteger(R.styleable.EqualRatioImageView_isRatio, 1);
        isRatio = (r == 1);
        ratio = new Float(height.floatValue() / width.floatValue());
        a.recycle();
    }

    public EqualRatioImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public void updateRatio(float ratio) {
        updateRatio(ratio, 0);
    }

    public void enableRatio(boolean bo) {
        if (isRatio == bo) {
            return;
        }
        isRatio = bo;
        postInvalidate();
    }

    public void updateRatio(float ratio, float max) {
        if (max > 0) {
            this.max = max;
        }
        this.ratio = getRatio(this.ratio);
        if (Math.abs(this.ratio - ratio) <= 0.1) {
            return;
        }
        this.ratio = getRatio(ratio);
        postInvalidate();
    }

    float getRatio(float ratio) {
        if (ratio < 0.43) {
            return 0.43f;
        } else if (ratio > max) {
            return max;
        } else {
            return ratio;
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isRatio) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        // For simple implementation, or internal size is always 0.
        // We depend on the container to specify the layout size of
        // our view. We can't really know what it is since we will be
        // adding and removing different arbitrary views and do not
        // want the layout to change as this happens.
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec),
                getDefaultSize(0, heightMeasureSpec));

        // Children are just made to fill our space.
        int childWidthSize = getMeasuredWidth();
        // 高度和宽度等比例
        widthMeasureSpec = MeasureSpec.makeMeasureSpec(
                childWidthSize, MeasureSpec.EXACTLY);
        if (childWidthSize == 0) {
            int h = getMeasuredHeight();
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
            widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (h / ratio), MeasureSpec.EXACTLY);
        } else {
            if (dependOnX) {
                heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * ratio), MeasureSpec.EXACTLY);
            } else {
                int h = getMeasuredHeight();
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
                widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (h / ratio), MeasureSpec.EXACTLY);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    protected boolean dependOnX = true;

    public void setDependOnX(boolean bo) {
        if (dependOnX == bo) {
            return;
        }
        dependOnX = bo;
        postInvalidate();
    }
}
